#include <olectl.h>
#include "IPEditUI.h"
#define SetadAll(Code) {for(int i = 0; i < 7; ++i){if ( GetItemAt(i)){if (i%2 == 0){((CIPEditUI*)GetItemAt(i))->Code;}else{((CLabelUI*)GetItemAt(i))->Code;}}}}


class CIPEditWnd : public CWindowWnd
{

public:
	CIPEditWnd();
	void Init(CIPEditUI* pOwner);


	RECT CalPos();
	LPCTSTR GetWindowClassName() const;
	LPCTSTR GetSuperClassName() const;
	void OnFinalMessage(HWND hWnd);
	
	LRESULT HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam);
	LRESULT OnKillFocus(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
	LRESULT OnEditChanged(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
	LRESULT OnChar(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
	void ToNext(bool bReverse = false);
	RECT GetItemPosByIndex(RECT rcOwner,int nIndex);
protected:
	CIPEditUI* m_pOwner;
	HBRUSH m_hBkBrush;
	bool m_bInit;
private:
	bool m_bInRange;
};


CIPEditWnd::CIPEditWnd() : m_pOwner(NULL), m_hBkBrush(NULL), m_bInit(false) ,m_bInRange(true)
{
}

void CIPEditWnd::Init(CIPEditUI* pOwner)
{
	m_pOwner = pOwner;
	RECT rcPos = CalPos();
	UINT uStyle = WS_CHILD | ES_AUTOHSCROLL|ES_CENTER ;
	Create(m_pOwner->GetManager()->GetPaintWindow(), NULL, uStyle, 0, rcPos);
	HFONT hFont=NULL;
	int iFontIndex=m_pOwner->GetFont();
	if (iFontIndex!=-1)
	{
		hFont=m_pOwner->GetManager()->GetFont(iFontIndex);
	}
	if (hFont==NULL)
	{
		hFont=m_pOwner->GetManager()->GetDefaultFontInfo()->hFont;
	}

	SetWindowFont(m_hWnd, hFont, TRUE);
	Edit_LimitText(m_hWnd, 3);//m_pOwner->GetMaxChar());

	Edit_SetText(m_hWnd, m_pOwner->GetText());
	Edit_SetModify(m_hWnd, FALSE);
	SendMessage(EM_SETMARGINS, EC_LEFTMARGIN | EC_RIGHTMARGIN, MAKELPARAM(0, 0));
	Edit_Enable(m_hWnd, m_pOwner->IsEnabled() == true);
	//Styls
	LONG styleValue = ::GetWindowLong(m_hWnd, GWL_STYLE);
	styleValue |= pOwner->GetWindowStyls();
	::SetWindowLong(GetHWND(), GWL_STYLE, styleValue);
	::ShowWindow(m_hWnd, SW_SHOWNOACTIVATE);
	::SetFocus(m_hWnd);
	m_bInit = true;    
}

RECT CIPEditWnd::CalPos()
{
	CDuiRect rcPos = m_pOwner->GetPos();
	RECT rcInset = m_pOwner->GetTextPadding();
	rcPos.left += rcInset.left;
	rcPos.top += rcInset.top;
	rcPos.right -= rcInset.right;
	rcPos.bottom -= rcInset.bottom;
	LONG lEditHeight = m_pOwner->GetManager()->GetFontInfo(m_pOwner->GetFont())->tm.tmHeight;
	if( lEditHeight < rcPos.GetHeight() ) 
	{
		rcPos.top += (rcPos.GetHeight() - lEditHeight) / 2;
		rcPos.bottom = rcPos.top + lEditHeight;
	}
	return rcPos;
}

LPCTSTR CIPEditWnd::GetWindowClassName() const
{
	return _T("IPEditWnd");
}

LPCTSTR CIPEditWnd::GetSuperClassName() const
{
	return WC_EDIT;
}

void CIPEditWnd::OnFinalMessage(HWND /*hWnd*/)
{
	m_pOwner->Invalidate();
	if( m_hBkBrush != NULL ) 
	{
		::DeleteObject(m_hBkBrush);
	}
	m_pOwner->m_pWindow = NULL;
	delete this;
}

LRESULT CIPEditWnd::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	LRESULT lRes = 0;
	BOOL bHandled = TRUE;
	if( uMsg == WM_KILLFOCUS ) 
	{
		lRes = OnKillFocus(uMsg, wParam, lParam, bHandled);
	}
	else if( uMsg == OCM_COMMAND ) 
	{
		if( GET_WM_COMMAND_CMD(wParam, lParam) == EN_CHANGE ) 
		{
			lRes = OnEditChanged(uMsg, wParam, lParam, bHandled);
		}
		else if( GET_WM_COMMAND_CMD(wParam, lParam) == EN_UPDATE ) 
		{
			RECT rcClient;
			::GetClientRect(m_hWnd, &rcClient);
			::InvalidateRect(m_hWnd, &rcClient, FALSE);
		}
		
	}
	else if( uMsg == WM_KEYDOWN )
	{
		if (TCHAR(wParam) == VK_RETURN ) 
		{
			m_pOwner->GetManager()->SendNotify(m_pOwner, DUI_MSGTYPE_RETURN);
		}
		//else if (a)
		//{
		//	
		//	if (wParam )
		//	{
		//		bHandled = FALSE;
		//	}
		//	//TCHAR szText[64];
		//	//_stprintf_s(szText,sizeof(szText)/sizeof(TCHAR),_T("%u"),lParam);
		//	//int nLen =Edit_GetTextLength(m_hWnd);
		//	//TCHAR * szText = new TCHAR[nLen + 1];
		//	//Edit_GetText(m_hWnd,szText,nLen + 1);
		//	//if (nLen > 0 && wParam == 'A')
		//	//{

		//	//	szText[nLen - 1] = 0;
		//	//}
		//	//Edit_SetText(m_hWnd ,szText);
		//	//Edit_SetSel(m_hWnd,nLen,nLen);
		//	//delete [] szText;
		//	//MessageBox(m_hWnd,szText ,_T(""),MB_OK);
		//}
		////else if (wParam == VK_BACK)
		////{
		////	bHandled = FALSE;
		////}
		else if(wParam == VK_LEFT)
		{
			if (LOWORD(Edit_GetSel(m_hWnd)) == 0)
			{
				ToNext(true);
			}
			else
			{
				bHandled =FALSE;
			}
		}
		else if(wParam == VK_RIGHT)
		{
			if (LOWORD(Edit_GetSel(m_hWnd)) == Edit_GetTextLength(m_hWnd))
			{
				ToNext();
				Edit_SetSel(m_hWnd ,0,0);
			}
			else
			{
				bHandled =FALSE;
			}
		}
		else if(wParam == VK_DELETE)
		{
			bHandled = FALSE;
		}
	}
	else if( uMsg == OCM__BASE + WM_CTLCOLOREDIT  || uMsg == OCM__BASE + WM_CTLCOLORSTATIC ) 
	{
		if( m_pOwner->GetNativeEditBkColor() == 0xFFFFFFFF ) 
		{
			//return NULL;
		}
		::SetBkMode((HDC)wParam, TRANSPARENT);
		DWORD dwTextColor = m_pOwner->GetTextColor();
		::SetTextColor((HDC)wParam, RGB(GetBValue(dwTextColor),GetGValue(dwTextColor),GetRValue(dwTextColor)));
		if( m_hBkBrush == NULL ) 
		{
			DWORD clrColor = m_pOwner->GetNativeEditBkColor();
			m_hBkBrush = ::CreateSolidBrush(RGB(GetBValue(clrColor), GetGValue(clrColor), GetRValue(clrColor)));
		}
		return (LRESULT)m_hBkBrush;
	}
	else if (uMsg == WM_CHAR)
	{
		OnChar(uMsg ,wParam ,lParam ,bHandled);

	}
	else
	{
		bHandled = FALSE;
	}
	if( !bHandled ) 
	{
		return CWindowWnd::HandleMessage(uMsg, wParam, lParam);
	}
	return lRes;
}

LRESULT CIPEditWnd::OnKillFocus(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
	LRESULT lRes = ::DefWindowProc(m_hWnd, uMsg, wParam, lParam);
	PostMessage(WM_CLOSE);
	return lRes;
}

LRESULT CIPEditWnd::OnEditChanged(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
	if( !m_bInit ) 
	{
		return 0;
	}
	if( m_pOwner == NULL ) 
	{
		return 0;
	}
	// Copy text back
			HDC hDC = GetDC(m_hWnd);
		::SetTextColor(hDC,m_pOwner->m_dwTextColor);
		ReleaseDC(m_hWnd ,hDC);
	int cchLen = ::GetWindowTextLength(m_hWnd) + 1;
	LPTSTR pstr = static_cast<LPTSTR>(_alloca(cchLen * sizeof(TCHAR)));
	ASSERT(pstr);
	if( pstr == NULL ) return 0;
	::GetWindowText(m_hWnd, pstr, cchLen);
	m_pOwner->m_sText = pstr;
	m_pOwner->GetManager()->SendNotify(m_pOwner, DUI_MSGTYPE_TEXTCHANGED);
	return 0;
}

RECT CIPEditWnd::GetItemPosByIndex(RECT rcOwner,int nIndex)
{
	int nWidth = rcOwner.right - rcOwner.left;
	int nHeight = rcOwner.bottom - rcOwner.top ;
	int nItem = (nWidth - 5 *3 )/4;
	RECT rcPos={0};
	rcPos.left = nItem*nIndex + 5 *nIndex +rcOwner.left;
	rcPos.top = rcOwner.top;
	rcPos.right = rcPos.left + nItem;
	rcPos.bottom = rcOwner.bottom;
	return rcPos;
}

void CIPEditWnd::ToNext( bool bReverse)
{
	if (bReverse)
	{
		if(m_pOwner->m_pFrontEdit)
		{
			m_pOwner->m_pFrontEdit->SetFocus();
			TCHAR szWord[64];
			TCHAR* szText = szWord+5;
			_tcscpy_s(szText ,59,m_pOwner->m_pFrontEdit->GetText());
			int nLen = _tcslen(szText) ;
			//szText[nLen] = 0;
			//m_pOwner->m_pFrontEdit->SetText(szText);
			m_pOwner->m_pFrontEdit->SetSel(nLen ,nLen);
		}

	}
	else
	{
		if ( m_pOwner->m_pNextEdit)
		{
			m_pOwner->m_pNextEdit->SetFocus();
			m_pOwner->m_pNextEdit->SetSel(0,-1);
		}
	}
}

LRESULT CIPEditWnd::OnChar(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
	if ((wParam >= '0'&& wParam <= '9') || wParam ==VK_BACK ||wParam == VK_LEFT ||wParam == VK_RIGHT)
	{
		bHandled = FALSE;
	}
	if (wParam == '.')
	{
		if (m_bInRange)
		{
			if ( !(HIWORD(Edit_GetSel(m_hWnd))-LOWORD(Edit_GetSel(m_hWnd))) &&Edit_GetTextLength(m_hWnd) > 0 )
			{
				ToNext();
			}

		}
		else if( LOWORD(Edit_GetSel(m_hWnd)) == Edit_GetTextLength(m_hWnd))  
		{
			ToNext();
		}

		
	}
	else if (wParam == VK_BACK)
	{
		if (LOWORD(Edit_GetSel(m_hWnd)) == 0 && m_bInRange)
		{
			ToNext(true);
		}
	}

	else if (Edit_GetTextLength(m_hWnd) > 1)// && !(HIWORD(Edit_GetSel(m_hWnd))-LOWORD(Edit_GetSel(m_hWnd)))  )
	{
		TCHAR szText[64];
		Edit_GetText(m_hWnd,szText,sizeof(szText)/sizeof(TCHAR));
		if (Edit_GetTextLength(m_hWnd) == 2 || (Edit_GetTextLength(m_hWnd) == 3 && HIWORD(Edit_GetSel(m_hWnd))-LOWORD(Edit_GetSel(m_hWnd))))
		{
			szText[LOWORD(Edit_GetSel(m_hWnd))] = wParam;
			szText[3] = 0;
		}

		if (HIWORD(Edit_GetSel(m_hWnd))-LOWORD(Edit_GetSel(m_hWnd)))
		{
			szText[LOWORD(Edit_GetSel(m_hWnd))] = wParam;
			szText[LOWORD(Edit_GetSel(m_hWnd)) + 1] = szText [HIWORD(Edit_GetSel(m_hWnd))  ];
			szText[LOWORD(Edit_GetSel(m_hWnd)) + 2] = szText [HIWORD(Edit_GetSel(m_hWnd)) + 1];
		}
			
		if (_tstoi(szText) >= 255 )
		{
			Edit_SetText(m_hWnd ,_T("255"));
			m_bInRange = false;
		}
		else 
		{
			m_bInRange = true;
			if (Edit_GetTextLength(m_hWnd)== 2&&LOWORD(Edit_GetSel(m_hWnd)) == 2 || (Edit_GetTextLength(m_hWnd)== 3&&LOWORD(Edit_GetSel(m_hWnd)) == 3))
			{
				ToNext();
			}
		}
	
	}
	return 0;
}

//***********************************************************************************
//
// CIPEditUI 
//***********************************************************************************

CIPEditUI::CIPEditUI() : m_pWindow(NULL),m_dwTextColor(0xFF000000)
	, m_uButtonState(0), 
	m_dwEditbkColor(0xFFFFFFFF), m_iWindowStyls(0)
	,m_pNextEdit(NULL),m_pFrontEdit(NULL)
{
	SetTextPadding(CDuiRect(4, 3, 4, 3));
	SetBkColor(0xFFFFFFFF);
	this->SetTransShadow(50);
	this->SetTransStroke(50);
	SetTextStyle(5);

}

UINT CIPEditUI::GetControlFlags() const
{
	if( !IsEnabled() ) 
	{
		return CControlUI::GetControlFlags();
	}
	return UIFLAG_SETCURSOR | UIFLAG_TABSTOP;
}

LPCTSTR CIPEditUI::GetClass() const
{
	return _T("IPEditUI");
}

void CIPEditUI::DoEvent(TEventUI& event)
{
	if( !IsMouseEnabled() && event.Type > UIEVENT__MOUSEBEGIN && event.Type < UIEVENT__MOUSEEND ) 
	{
		if( m_pParent != NULL ) m_pParent->DoEvent(event);
		else CLabelUI::DoEvent(event);
		return;
	}

	if( event.Type == UIEVENT_SETCURSOR && IsEnabled() )
	{
		::SetCursor(::LoadCursor(NULL, MAKEINTRESOURCE(IDC_IBEAM)));
		return;
	}
	if( event.Type == UIEVENT_WINDOWSIZE )
	{
		if( m_pWindow != NULL ) m_pManager->SetFocusNeeded(this);
	}
	if( event.Type == UIEVENT_SCROLLWHEEL )
	{
		if( m_pWindow != NULL ) return;
	}
	if( event.Type == UIEVENT_SETFOCUS && IsEnabled() ) 
	{
		SetTextColor(m_dwTextColor);
		if( m_pWindow ) return;
		m_pWindow = new CIPEditWnd();
		ASSERT(m_pWindow);
		m_pWindow->Init(this);
		
		Invalidate();
	}
	if( event.Type == UIEVENT_KILLFOCUS && IsEnabled() ) 
	{
		Invalidate();
	}
	if( event.Type == UIEVENT_BUTTONDOWN || event.Type == UIEVENT_DBLCLICK || event.Type == UIEVENT_RBUTTONDOWN) 
	{
		if( IsEnabled() ) {
			GetManager()->ReleaseCapture();
			if( IsFocused() && m_pWindow == NULL )
			{
				m_pWindow = new CIPEditWnd();
				ASSERT(m_pWindow);
				m_pWindow->Init(this);
				if( PtInRect(&m_rcItem, event.ptMouse) )
				{
					int nSize = GetWindowTextLength(*m_pWindow);
					if( nSize == 0 )
						nSize = 1;

					Edit_SetSel(*m_pWindow, 0, nSize);
				}
			}
			else if( m_pWindow != NULL )
			{
#if 1			
				int nSize = GetWindowTextLength(*m_pWindow);
				if( nSize == 0 )
					nSize = 1;

				Edit_SetSel(*m_pWindow, 0, nSize);
#else
				POINT pt = event.ptMouse;
				pt.x -= m_rcItem.left + m_rcTextPadding.left;
				pt.y -= m_rcItem.top + m_rcTextPadding.top;
				::SendMessage(*m_pWindow, WM_LBUTTONDOWN, event.wParam, MAKELPARAM(pt.x, pt.y));
#endif
			}
		}
		return;
	}
	if( event.Type == UIEVENT_MOUSEMOVE ) 
	{
		return;
	}
	if( event.Type == UIEVENT_BUTTONUP ) 
	{
		return;
	}
	if( event.Type == UIEVENT_CONTEXTMENU )
	{
		return;
	}
	if( event.Type == UIEVENT_MOUSEENTER )
	{
		if( IsEnabled() ) 
		{
			m_uButtonState |= UISTATE_HOT;
			Invalidate();
		}
		return;
	}
	if( event.Type == UIEVENT_MOUSELEAVE )
	{
		if( IsEnabled() ) 
		{
			m_uButtonState &= ~UISTATE_HOT;
			Invalidate();
		}
		return;
	}
	CLabelUI::DoEvent(event);
}

void CIPEditUI::SetEnabled(bool bEnable)
{
	CControlUI::SetEnabled(bEnable);
	if( !IsEnabled() ) 
	{
		m_uButtonState = 0;
	}
}

void CIPEditUI::SetText(LPCTSTR pstrText)
{
	m_sText = pstrText;
	if( m_pWindow != NULL ) 
	{
		Edit_SetText(*m_pWindow, m_sText);
	}
	Invalidate();
}


int CIPEditUI::GetWindowStyls() const 
{
	return m_iWindowStyls;
}

//LPCTSTR CIPEditUI::GetNormalImage()
//{
//	return m_sNormalImage;
//}
//
//void CIPEditUI::SetNormalImage(LPCTSTR pStrImage)
//{
//	m_sNormalImage = pStrImage;
//	Invalidate();
//}
//
//LPCTSTR CIPEditUI::GetHotImage()
//{
//	return m_sHotImage;
//}
//
//void CIPEditUI::SetHotImage(LPCTSTR pStrImage)
//{
//	m_sHotImage = pStrImage;
//	Invalidate();
//}
//
//LPCTSTR CIPEditUI::GetFocusedImage()
//{
//	return m_sFocusedImage;
//}
//
//void CIPEditUI::SetFocusedImage(LPCTSTR pStrImage)
//{
//	m_sFocusedImage = pStrImage;
//	Invalidate();
//}
//
//LPCTSTR CIPEditUI::GetDisabledImage()
//{
//	return m_sDisabledImage;
//}
//
//void CIPEditUI::SetDisabledImage(LPCTSTR pStrImage)
//{
//	m_sDisabledImage = pStrImage;
//	Invalidate();
//}
//
void CIPEditUI::SetNativeEditBkColor(DWORD dwBkColor)
{
	m_dwEditbkColor = dwBkColor;
}

DWORD CIPEditUI::GetNativeEditBkColor() const
{
	return m_dwEditbkColor;
}

void CIPEditUI::SetSel(long nStartChar, long nEndChar)
{
	if( m_pWindow != NULL ) Edit_SetSel(*m_pWindow, nStartChar,nEndChar);
}

//void CIPEditUI::SetSelAll()
//{
//	SetSel(0,-1);
//}

void CIPEditUI::SetReplaceSel(LPCTSTR lpszReplace)
{
	if( m_pWindow != NULL ) Edit_ReplaceSel(*m_pWindow, lpszReplace);
}

void CIPEditUI::SetPos(RECT rc)
{
	CControlUI::SetPos(rc);
	if( m_pWindow != NULL ) 
	{
		RECT rcPos = m_pWindow->CalPos();
		::SetWindowPos(m_pWindow->GetHWND(), NULL, rcPos.left, rcPos.top, rcPos.right - rcPos.left, 
			rcPos.bottom - rcPos.top, SWP_NOZORDER | SWP_NOACTIVATE);        
	}
}

void CIPEditUI::SetVisible(bool bVisible)
{
	CControlUI::SetVisible(bVisible);
	if( !IsVisible() && m_pWindow != NULL ) m_pManager->SetFocus(NULL);
}

void CIPEditUI::SetInternVisible(bool bVisible)
{
	if( !IsVisible() && m_pWindow != NULL ) m_pManager->SetFocus(NULL);
}

SIZE CIPEditUI::EstimateSize(SIZE szAvailable)
{
	if( m_cxyFixed.cy == 0 ) 
	{
		return CSize(m_cxyFixed.cx, m_pManager->GetFontInfo(GetFont())->tm.tmHeight + 6);
	}
	return CControlUI::EstimateSize(szAvailable);
}

void CIPEditUI::PaintStatusImage(HDC hDC)
{
	if( IsFocused() ) m_uButtonState |= UISTATE_FOCUSED;
	else m_uButtonState &= ~ UISTATE_FOCUSED;
	if( !IsEnabled() ) m_uButtonState |= UISTATE_DISABLED;
	else m_uButtonState &= ~ UISTATE_DISABLED;

	if( (m_uButtonState & UISTATE_DISABLED) != 0 ) 
	{
		if( !m_sDisabledImage.IsEmpty() ) 
		{
			if( !DrawImage(hDC, (LPCTSTR)m_sDisabledImage) ) m_sDisabledImage.Empty();
			else return;
		}
	}
	else if( (m_uButtonState & UISTATE_FOCUSED) != 0 ) 
	{
		if( !m_sFocusedImage.IsEmpty() ) 
		{
			if( !DrawImage(hDC, (LPCTSTR)m_sFocusedImage) ) m_sFocusedImage.Empty();
			else return;
		}
	}
	else if( (m_uButtonState & UISTATE_HOT) != 0 ) 
	{
		if( !m_sHotImage.IsEmpty() ) 
		{
			if( !DrawImage(hDC, (LPCTSTR)m_sHotImage) ) m_sHotImage.Empty();
			else return;
		}
	}

	if( !m_sNormalImage.IsEmpty() ) 
	{
		if( !DrawImage(hDC, (LPCTSTR)m_sNormalImage) ) m_sNormalImage.Empty();
		else return;
	}
}

void CIPEditUI::PaintText(HDC hDC)
{
	if( m_dwTextColor == 0 ) 
	{
		m_dwTextColor = m_pManager->GetDefaultFontColor();
	}
	if( m_dwDisabledTextColor == 0 ) 
	{
		m_dwDisabledTextColor = m_pManager->GetDefaultDisabledColor();
	}
	if( m_sText.IsEmpty() ) 
	{
		return;
	}
	CDuiString sText = m_sText;

	RECT rc = m_rcItem;
	rc.left += m_rcTextPadding.left;
	rc.right -= m_rcTextPadding.right;
	rc.top += m_rcTextPadding.top;
	rc.bottom -= m_rcTextPadding.bottom;
	if( IsEnabled() )
	{
		CRenderEngine::DrawText(hDC, m_pManager, rc, sText, m_dwTextColor, \
			m_iFont, DT_SINGLELINE | m_uTextStyle);
	}
	else 
	{
		CRenderEngine::DrawText(hDC, m_pManager, rc, sText, m_dwDisabledTextColor, \
			m_iFont, DT_SINGLELINE | m_uTextStyle);

	}
	
}

void CIPEditUI::SetNextEdit(CIPEditUI*& pEdit)
{
	m_pNextEdit = pEdit;
	pEdit->SetFrontEdit(this);
}

void CIPEditUI::SetFrontEdit(CIPEditUI* pEdit)
{
	m_pFrontEdit = pEdit;
}

void CIPEditUI::SetTextColor(DWORD dwColor)
{
	m_dwTextColor = dwColor;
	CLabelUI::SetTextColor(dwColor);
	if (m_pWindow)
	{
		HDC hDC = GetDC(m_pWindow->GetHWND());
		::SetTextColor(hDC,dwColor);
		ReleaseDC(m_pWindow->GetHWND() ,hDC);
	}
	
}

//**********************************************
//
//CIPAddress
//**********************************************


CIPAddress::CIPAddress()
	:m_nWidth(0)
	,m_nHeight(0)
{
	InitEditItem();
	//SetBkColor(0xFFFFFFFF);
}

void CIPAddress::InitEditItem()
{
	CContainerUI::RemoveAll();
	RECT rcLabel={0};
	CLabelUI* pTemp = NULL;
	for (int i = 0; i < 4;++i )
	{
		if (i > 0)
		{
			pTemp = new CLabelUI;
			CContainerUI::Add(pTemp);
			pTemp->SetFont(1);
			pTemp->SetText(_T("."));
			pTemp->SetBkColor(0xFFFFFFFF);
			pTemp->SetFixedWidth(DotWidth);
			pTemp->SetFloat(true);
			pTemp->SetPos(rcLabel);
			
		}
		m_pEditItem[i] = new CIPEditUI;
		CContainerUI::Add(m_pEditItem[i]);
		rcLabel = ResetItem(m_pEditItem[i],i);
		if (i > 0 )
		{
			m_pEditItem[i - 1]->SetNextEdit(m_pEditItem[i]);
		}

	}
}

void CIPAddress::AdjustPos()
{
	RECT rcLabel;
	CIPEditUI* pEdit = NULL;
	CLabelUI* pLabel = NULL;
	for(int i = 0 ,j=0; i < 4  ;++i)
	{
		if (i > 0)
		{
			pLabel = (CLabelUI*)CContainerUI::GetItemAt(i*2 - 1);
			if (pLabel)
			{
				pLabel->SetPos(rcLabel);
			}
		}
		pEdit = (CIPEditUI*)CContainerUI::GetItemAt(i*2);
		if (pEdit)
		{
			rcLabel = ResetItem(pEdit ,i);
		}
	}
}

LPCTSTR CIPAddress::GetClass()const
{
	return _T("IPAddress");
}

CDuiString CIPAddress::GetIPText()const
{
	TCHAR szIP[64];
	ZeroMemory(szIP,sizeof(szIP));
	for(int i = 0 ;i < 4 ;++i)
	{
		if (i > 0) _tcscat_s(szIP,sizeof(szIP)/sizeof(TCHAR),_T("."));
		_tcscat_s(szIP,sizeof(szIP)/sizeof(TCHAR),m_pEditItem[i]->GetText());
		
	}
	return szIP;
}

UINT CIPAddress::GetIP()const
{
	UINT nIP = -1;
	for(int i = 0 ;i < 4 ;++i)
	{
		nIP &= _tcstoul(m_pEditItem[i]->GetText(),NULL ,10) >> ((3 - i) * 8);
	}
	return nIP;
}

void CIPAddress::SetIPText(UINT nIP)
{
	TCHAR szIP[64];
	for(int i = 0 ;i < 4 ;++i)
	{
		_stprintf_s(szIP,sizeof(szIP)/sizeof(TCHAR) ,_T("%d"), (nIP<<(3-i*8)) & 0xFF);
		m_pEditItem[i]->SetText(szIP);
	}
}

void CIPAddress::SetAttribute(LPCTSTR pstrName, LPCTSTR pstrValue)
{
	if(_tcscmp(pstrName , _T("bkcolor") ) == 0)
	{
        while( *pstrValue > _T('\0') && *pstrValue <= _T(' ') ) 
		{
			pstrValue = ::CharNext(pstrValue);
		}
        if( *pstrValue == _T('#')) pstrValue = ::CharNext(pstrValue);
        LPTSTR pstr = NULL;
        DWORD dwColor = _tcstoul(pstrValue, &pstr, 16);
        SetBkColor(dwColor);
	}
	else if (_tcscmp(pstrName ,_T("inset")) == 0);
	else if (_tcscmp(pstrName ,_T("pos")) == 0)
	{
        RECT rcPos = { 0 };
        LPTSTR pstr = NULL;
        rcPos.left = _tcstol(pstrValue, &pstr, 10);  ASSERT(pstr);    
        rcPos.top = _tcstol(pstr + 1, &pstr, 10);    ASSERT(pstr);    
        rcPos.right = _tcstol(pstr + 1, &pstr, 10);  ASSERT(pstr);    
        rcPos.bottom = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);    
        SIZE szXY = {rcPos.left >= 0 ? rcPos.left : rcPos.right, rcPos.top >= 0 ? rcPos.top : rcPos.bottom};
        SetFixedXY(szXY);
        SetFixedWidth(rcPos.right - rcPos.left);
        SetFixedHeight(rcPos.bottom - rcPos.top);
	}
	else if (_tcscmp(pstrName ,_T("font")) == 0)
	{
		SetFont(_tstoi(pstrValue));
	}
	else if (_tcscmp(pstrName ,_T("textcolor")) == 0)
	{
        while( *pstrValue > _T('\0') && *pstrValue <= _T(' ') ) 
		{
			pstrValue = ::CharNext(pstrValue);
		}
        if( *pstrValue == _T('#')) pstrValue = ::CharNext(pstrValue);
        LPTSTR pstr = NULL;
        DWORD dwColor = _tcstoul(pstrValue, &pstr, 16);
		SetTextColor(dwColor);
	}
	else if (_tcscmp(pstrName ,_T("bkimage")) == 0)
	{
		SetBkImage(pstrValue);
	}
	else 
	{
		CContainerUI::SetAttribute(pstrName ,pstrValue);
	}
}
void CIPAddress::SetPos(RECT rcPos)
{
	CContainerUI::SetPos(rcPos);
	AdjustPos();
}

void CIPAddress::SetBkColor(DWORD dwColor)
{
	CContainerUI::SetBkColor(dwColor);
	for(int i = 0; i < 4 ;++i)
	{
		m_pEditItem[i]->SetBkColor(dwColor);
		m_pEditItem[i]->SetNativeEditBkColor(dwColor);
		if (GetItemAt(i*2+1)) GetItemAt(i*2+1)->SetBkColor(dwColor);
	}
}

RECT CIPAddress::ResetItem(CIPEditUI*  pItem,int nIndex)
{
	RECT rcPanel = CContainerUI::GetPos();
	
	rcPanel.left += m_nBorderSize;
	rcPanel.top += m_nBorderSize;
	rcPanel.bottom -= m_nBorderSize;
	rcPanel.right -= m_nBorderSize;
	int nWidth = rcPanel.right - rcPanel.left;
	int nItem = 0;
	if (nWidth > 0)
	{
		nItem = ((nWidth - DotWidth*3)%4) * 1. < 0.5 ? (nWidth - DotWidth*3)/4+ 1:(nWidth - DotWidth*3)/4  ;
	}
	rcPanel.left += nIndex* nItem + nIndex* DotWidth ;
	rcPanel.right = rcPanel.left + nItem;
	pItem->SetFloat(true);
	pItem->SetPos(rcPanel);
	rcPanel.left = rcPanel.right ;
	rcPanel.right = rcPanel.left + DotWidth;
	return rcPanel;
}

void CIPAddress::SetFont(int nFont)
{
	for (int i = 0 ;i < 4 ;++i)
	{
		m_pEditItem[i]->SetFont(nFont);
	}
}

void CIPAddress::SetTextColor(DWORD dwColor)
{
	for (int i = 0 ;i < 4 ;++i)
	{
		m_pEditItem[i]->SetTextColor(dwColor);
	}	
}

void CIPAddress::SetBkImage(LPCTSTR pstrImage)
{
	CContainerUI::SetBkImage(pstrImage);
	
	//TCHAR szImage[256] ;
	//int nWidth;
	//int nStart =  - nWidth;
	//for (int i = 0; i < 4 ; ++i)
	//{
	//	nStart = nStart + nWidth;
	//	_stprintf(szImage ,_T("bkimage='button_normal.png' source='%d,%d,%d,%d'") ,nStart,-1,nWidth ,-1);
	//	m_pEditItem[i*2]->SetBkImage(szImage);	
	//	if (i < 3)
	//	{
	//		nStart = nStart + nWidth;
	//		_stprintf(szImage ,_T("bkimage='button_normal.png' source='%d,%d,%d,%d'") ,nStart,-1,DotWidth ,-1);
	//		m_pEditItem[i*2+1]->SetBkImage(szImage);	
	//	}

	//}
	
}

//void CIPAddress::DoEvent(TEventUI& event)
//{
//	if (event.Type == UIEVENT__FIRST)
//	{
//		MessageBox(NULL,_T(""),_T(""),MB_OK);
//	}
//	else
//	{
//		CContainerUI::DoEvent(event);
//	}
//}